package operation;
import book.Book;
import book.Booklist;
import jdk.swing.interop.SwingInterOpUtils;

import java.util.Scanner;

public class DelOperatioon implements IOPeration{
    public void work(Booklist booklist){
        System.out.println("删除图书。。。。。");
        System.out.println("请输入要删除的书名");
        Scanner scanner =new Scanner(System.in);
        String bookName=scanner.nextLine();
        int currentSize= booklist.getUsedSize();
        int pos=-1;
        int i=0;
        for( i=0;i<currentSize;i++){
            Book book=booklist.getBook(i);
            if(book.getName().equals(bookName)){
                pos=i;
                break;
            }

        }
        //
        if(i>=currentSize){
            System.out.println("未找到该书");
            return ;
        }

            //说明找到了书  开始删除
        for(int j=pos;j<currentSize-1;j++){
            Book book=booklist.getBook(j+1);
            booklist.setBook(j,book);
        }
        //修改usedSize
        booklist.setUsedSize(currentSize-1);
        booklist.setBook(currentSize-1,null);
        System.out.println("删除成功");
    }
}
